using Assets.Scripts.System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(Collider2D))]
public class CollisionEffect : LogicalEffectBase
{
    private CollisionEffectData data;
    private new Collider2D collider2D;
    private AttributeData originalGOData;
    public override void Init(GameObject originalGameObject, params object[] args)
    {
        base.Init(originalGameObject, args);
        data = effectData as CollisionEffectData;
        collider2D = GetComponent<Collider2D>();
        originalGOData = isFromPlayer ? PlayerSystem.Instance.data : GetComponent<EnemyBase>().data;
    }
    public override void OnApply()
    {

    }
    public override void OnUpdate()
    {
        if (collider2D.IsTouchingLayers(data.collisionMask) || data.lifeTime <= 0)
        {
            OnTouchObstacle();
        }
        data.lifeTime -= Time.deltaTime;
    }
    public override void OnComplete()
    {
        foreach (var item in data.collsionEffectsList)
        {
            EffectSystem.Instance.GetParticleEffectByID(item, transform.position, Vector3.zero, Vector3.one, null, true);
        }
        effectManager.DestroyEffect();
        base.OnComplete();

    }
    void OnTouchObstacle()
    {
        SetLifeState(false);
    }
    void OnTouchHitTarget(Collider2D collision)
    {
        if (data.hpChangeValue * originalGOData.GetRecoverScale() > 0)
        {
            collision.gameObject.GetComponent<HealthControllerBase>()?.Recover(data.hpChangeValue * originalGOData.GetRecoverScale());
        }
        else if (data.hpChangeValue * originalGOData.GetDamageMultiplier() < 0)
        {
            collision.gameObject.GetComponent<HealthControllerBase>()?.Hit(-data.hpChangeValue * originalGOData.GetDamageMultiplier());
        }
        if (!data.isPenetrate)
        {
            foreach (var item in data.collsionEffectsList)
            {
                EffectSystem.Instance.GetParticleEffectByID(item, transform.position, Vector3.zero, Vector3.one, null, true);
            }
            SetLifeState(false);
            effectManager.DestroyEffect();
        }
    }
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collider2D.IsTouchingLayers(data.hitMask))
        {
            OnTouchHitTarget(collision);
        }
    }
}
